<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/4/26
 * Time: 21:08
 */

namespace app\model\business;

use app\base\BaseModel;
use think\model\concern\SoftDelete;

class BusinessModel extends BaseModel
{
    use SoftDelete;

    protected $name = 'business';

    /**
     * 申请商家认证
     * @param $user_id
     * @param $name
     * @param $storefront
     * @param $contact_name
     * @param $contact_phone
     * @param $address
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function addBusiness($user_id, $name, $storefront, $contact_name, $contact_phone, $address)
    {
        $data = self::where(['user_id' => $user_id])->find();
        if (empty($data)) {
            $in_data = [
                'user_id' => $user_id,
                'name' => $name,
                'storefront' => $storefront,
                'contact_name' => $contact_name,
                'contact_phone' => $contact_phone,
                'address' => $address,
                'update_time' => time(),
                'create_time' => time(),
            ];
            $ret = self::insert($in_data);
        } else {
            $up_data = [
                'user_id' => $user_id,
                'name' => $name,
                'storefront' => $storefront,
                'contact_name' => $contact_name,
                'contact_phone' => $contact_phone,
                'address' => $address,
                'check_status' => 0,
                'update_time' => time()
            ];
            $id = $data['id'];
            $ret = self::where(['id' => $id])->update($up_data);
        }
        if ($ret === false) {
            return false;
        }
        return true;
    }

    /**
     * 获取认证状态
     * @param $user_id
     * @return int|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getBusinessStatus($user_id)
    {
        $data = self::where(['user_id' => $user_id])->find();
        if (empty($data)) {
            return 4;
        }
        return $data['check_status'];
    }

}
